package com.tanhua.server.controller;


import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.holder.UserHolder;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/users")
public class SettingController {

    @Autowired
    private SettingsService settingsService;

    @Autowired
    private UserService userService;

    /**
     * 查询通用设置
     */
    @GetMapping("/settings")
    public ResponseEntity settings(){
        SettingsVo Vo = settingsService.setttings();
        return ResponseEntity.ok(Vo);
    }

    /**
     * 设置陌生人问题
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){
        String content = (String)map.get("content");
        settingsService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }

    //通知设置
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map){
        settingsService.saveSettings(map);
        return ResponseEntity.ok(null);
    }

    //分页查询黑名单
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") int page,
                                    @RequestParam(defaultValue = "10") int pageSize){
        //调用service查询
        PageResult pr = settingsService.blackliat(page,pageSize);
        return ResponseEntity.ok(null);
    }

    //取消黑名单
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity  deleteBlackList(@PathVariable("uid") Long uid){
        settingsService.deleteBlackList(uid);
        return ResponseEntity.ok(null);
    }


    /**
     * 修改手机号,发送验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        String mobile = UserHolder.getMobile();
        userService.sendVerificationCode(mobile);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号,校验验证码
     */
    @PostMapping("phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        String phone = (String)map.get("phone");
        String checkVerificationCode = (String) map.get("checkVerificationCode");
        Boolean aBoolean = userService.checkVerificationCode(phone, checkVerificationCode);
        return ResponseEntity.ok(aBoolean);
    }

    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map){
        userService.update(map);
        return ResponseEntity.ok(null);
    }

}
